#include "Program.h"
#include <vector>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

	CPU* Program::cpu;

Program::Program()
{
}

Program::~Program()
{
}

bool Program::loadProgram(string path)
{
	ifstream file;
	file.open(path.c_str(), fstream::in | fstream::out);
	int pos = 0;
	if(!file.is_open())
	{
		cout << " Program not found " << endl;
		return false;
	}
	while(!file.eof())
	{
		string line;
		unsigned short int lineHex;
		file >> line;
		
		if(line.substr(0,7)=="address")
		{
			stringstream s(stringstream::in | stringstream::out);
			s << line.substr(7);
			s >> hex >> pos;
		}
		else
		{
			// cout << " Read Line: " << line << endl;;
			stringstream s(stringstream::in | stringstream::out);
			s << line;
			s >> hex >> lineHex;
			Program::cpu->setMemory(pos,lineHex);
			pos++;
		}
	}
	file.close();
	return true; 	
}

void Program::setCPU(CPU* proc)
{
	Program::cpu = proc;
}

